import { existsSync, mkdirSync } from 'fs';
import { isAbsolute, join, parse } from 'path';

function getAbsolutelyPath(path: string) {
    return isAbsolute(path) ? path : join(process.cwd(), path);
}

function getFilePath(dir: string, fileName: string) {
    return join(getAbsolutelyPath(dir), fileName);
}

function mkdirSafetySync(path: string): boolean {
    let result = false;
    try {
        const parsedPath = parse(getAbsolutelyPath(path));

        if (!existsSync(parsedPath.dir)) {
            mkdirSync(parsedPath.dir, { recursive: true });

            //console.log('make dir success. path is ', parsedPath.dir);
            result = true;
        }
    } catch (err) {
        console.warn(`make dir error: `, err);
    }

    return result;
}

export { getAbsolutelyPath, getFilePath, mkdirSafetySync };
